#!/bin/bash -

# vim: ts=4 sts=4 sw=4 et si ai fenc=utf-8
#===============================================================================
# Description :
#      Author : root
#     Created : 2023-11-26 17:34
#===============================================================================
export LANG="en_US.UTF-8"
#set -o nounset

basedir=$(dirname "$(readlink -qe "${BASH_SOURCE[0]}")")

function PUSHD()
{
    pushd $1 >/dev/null

}

function POPD()
{
    local level=${1:-1}

    while [ ${level} -gt 0  ]; do
        popd >/dev/null || return 1
        (( level--  ))
    done
}

function count_line()
{
    wc -l ${1} 2>/dev/null | awk 'BEGIN{n=0} {n=$1} END {print n}'
}

function get_filelist()
{
    if [ -d "${1}" ]; then
        PUSHD ${1}
    else
        PUSHD "."
    fi

    local EXCH_DIR="/var/tmp"
    mkdir -p ${EXCH_DIR}
    local update_filelist="" update_dirlist=""
    local tmp_filelist="${EXCH_DIR}/.filelist_$$" tmp_dirlist="${EXCH_DIR}/.dirlist_$$"
    for cm in $(find . -type f -name CMakeLists.txt 2>/dev/null); do
        filepath=$(dirname "${cm}")
        if [ $(grep -c extract_source_list ${filepath}/CMakeLists.txt) -gt 0 ]; then
            PUSHD ${filepath}
            find . -path ./tmp -prune -o -path ./.cbuild -prune \
                -o \( -name "*.c" -o -name "*.h" -o -name "*.cc" -o -name "*.cpp" -o -name "*.hpp" \) \
                -type f -printf "%P\n" 2>/dev/null | sort -b -d -u >${tmp_filelist}
            if [ $(count_line ${tmp_filelist}) -gt 0 ]; then
                diff -q ${tmp_filelist} .filelist >/dev/null 2>&1
                if [ $? -ne 0 ]; then
                    mv -f ${tmp_filelist} .filelist
                    update_filelist="${update_filelist} ${filepath}/.filelist"
                    touch CMakeLists.txt
                else
                    rm -f ${tmp_filelist}
                fi
            else
                rm -f ${tmp_filelist} .filelist
            fi
            POPD
        else
            if [ -f ${filepath}/.filelist ]; then
                echo "rm -f ${filepath}/.filelist"
                rm -f ${filepath}/.filelist
                update_filelist="${update_filelist} ${filepath}/.filelist"
                touch CMakeLists.txt
            fi
        fi

        if [ $(grep -c add_sub_dir ${filepath}/CMakeLists.txt) -gt 0 ]; then
            PUSHD ${filepath}
            find . -mindepth 2 -maxdepth 2 -type f -name CMakeLists.txt -printf "%P\n" >${tmp_dirlist} 2>/dev/null
            if [ $(count_line ${tmp_dirlist}) -gt 0 ]; then
                xargs -a ${tmp_dirlist} dirname | sort -b -d -u >${tmp_dirlist}.1
                mv -f ${tmp_dirlist}.1 ${tmp_dirlist}

                diff -q ${tmp_dirlist} .dirlist >/dev/null 2>&1
                if [ $? -ne 0 ]; then
                    mv -f ${tmp_dirlist} .dirlist
                    update_dirlist="${update_dirlist} ${dirpath}/.dirlist"
                    touch CMakeLists.txt
                else
                    rm -f ${tmp_dirlist}
                fi
            else
                rm -f .dirlist ${tmp_dirlist}
            fi

            POPD
        else
            if [ -f ${filepath}/.dirlist ]; then
                echo "rm -f ${filepath}/.dirlist"
                rm -f ${filepath}/.dirlist
                update_dirlist="${update_dirlist} ${filepath}/.dirlist"
                touch CMakeLists.txt
            fi
        fi
    done

    if [ -n "${update_filelist}" ]; then
        echo "update filelist:${update_filelist}, cmake again"
    fi
    if [ -n "${update_dirlist}" ]; then
        echo "update dirlist:${update_dirlist}, cmake again"
    fi

    POPD
}

get_filelist $1
